package Views;

import Controllers.EmployeeCTR;
import Models.*;
import Views.TableModels.EmployeeTableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JOptionPane;

/**
 * @author Morten
 */
public class EmployeeView extends javax.swing.JFrame
{
	private EmployeeCTR controller = new EmployeeCTR();

    public EmployeeView()
	{
        initComponents();
		findTable.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseClicked(MouseEvent e){
				if (e.getClickCount() == 2)
				{
					if(findTable.getSelectedRowCount() == 1)
					{
						int chosenId = Integer.parseInt(findTable.getValueAt(findTable.getSelectedRow(), 0).toString());
						new EmployeeInfoView(controller.getEmployee(chosenId)).setVisible(true);
					}
				}
			}
		 });
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        findLocationBG = new javax.swing.ButtonGroup();
        addLocationBG = new javax.swing.ButtonGroup();
        employeeTabbedPane = new javax.swing.JTabbedPane();
        addEmployeePanel = new javax.swing.JPanel();
        addDescriptionLabel = new javax.swing.JLabel();
        addNameLabel = new javax.swing.JLabel();
        addNameTF = new javax.swing.JTextField();
        addLocationLabel = new javax.swing.JLabel();
        addLumberYardRB = new javax.swing.JRadioButton();
        addHardwareStoreRB = new javax.swing.JRadioButton();
        addButton = new javax.swing.JButton();
        findEmployeePanel = new javax.swing.JPanel();
        findDescriptionLabel = new javax.swing.JLabel();
        findNameLabel = new javax.swing.JLabel();
        findNameTF = new javax.swing.JTextField();
        findLocationLabel = new javax.swing.JLabel();
        findNowhereRB = new javax.swing.JRadioButton();
        findLumberYardRB = new javax.swing.JRadioButton();
        findHardwareStoreRB = new javax.swing.JRadioButton();
        findButton = new javax.swing.JButton();
        findScrollPane = new javax.swing.JScrollPane();
        findTable = new javax.swing.JTable();
        showAllbutton = new javax.swing.JButton();
        getEmployeePanel = new javax.swing.JPanel();
        getDescriptionLabel = new javax.swing.JLabel();
        getIdLabel = new javax.swing.JLabel();
        getIdSP = new javax.swing.JSpinner();
        getButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Ansatte");

        employeeTabbedPane.setPreferredSize(new java.awt.Dimension(490, 370));

        addEmployeePanel.setBackground(javax.swing.UIManager.getDefaults().getColor("TabRenderer.selectedActivatedForeground"));

        addDescriptionLabel.setText("Her kan der tilføjes ansatte");

        addNameLabel.setText("Navn:");

        addLocationLabel.setText("Afdeling:");

        addLocationBG.add(addLumberYardRB);
        addLumberYardRB.setSelected(true);
        addLumberYardRB.setText("Trælasthandel");

        addLocationBG.add(addHardwareStoreRB);
        addHardwareStoreRB.setText("Byggemarked");

        addButton.setText("Tilføj");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout addEmployeePanelLayout = new org.jdesktop.layout.GroupLayout(addEmployeePanel);
        addEmployeePanel.setLayout(addEmployeePanelLayout);
        addEmployeePanelLayout.setHorizontalGroup(
            addEmployeePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(addEmployeePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(addEmployeePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(addDescriptionLabel)
                    .add(addEmployeePanelLayout.createSequentialGroup()
                        .add(addEmployeePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(addLocationLabel)
                            .add(addNameLabel))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(addEmployeePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(addEmployeePanelLayout.createSequentialGroup()
                                .add(addLumberYardRB)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(addHardwareStoreRB))
                            .add(addNameTF, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)))
                    .add(addButton))
                .add(166, 166, 166))
        );
        addEmployeePanelLayout.setVerticalGroup(
            addEmployeePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(addEmployeePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(addDescriptionLabel)
                .add(18, 18, 18)
                .add(addEmployeePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(addNameLabel)
                    .add(addNameTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(addEmployeePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(addLocationLabel)
                    .add(addLumberYardRB)
                    .add(addHardwareStoreRB))
                .add(18, 18, 18)
                .add(addButton)
                .addContainerGap(212, Short.MAX_VALUE))
        );

        employeeTabbedPane.addTab("Tilføj", addEmployeePanel);

        findEmployeePanel.setBackground(javax.swing.UIManager.getDefaults().getColor("TabRenderer.selectedActivatedForeground"));

        findDescriptionLabel.setText("Her kan der søges på ansatte");

        findNameLabel.setText("Navn:");

        findLocationLabel.setText("Afdeling:");

        findLocationBG.add(findNowhereRB);
        findNowhereRB.setSelected(true);
        findNowhereRB.setText("-");

        findLocationBG.add(findLumberYardRB);
        findLumberYardRB.setText("Trælasthandel");

        findLocationBG.add(findHardwareStoreRB);
        findHardwareStoreRB.setText("Byggemarked");

        findButton.setText("Søg");
        findButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findButtonActionPerformed(evt);
            }
        });

        findTable.setModel(new Views.TableModels.EmployeeTableModel());
        findScrollPane.setViewportView(findTable);

        showAllbutton.setText("Vis alle");
        showAllbutton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showAllbuttonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout findEmployeePanelLayout = new org.jdesktop.layout.GroupLayout(findEmployeePanel);
        findEmployeePanel.setLayout(findEmployeePanelLayout);
        findEmployeePanelLayout.setHorizontalGroup(
            findEmployeePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(findEmployeePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(findEmployeePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, findScrollPane, 0, 0, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, findDescriptionLabel)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, findEmployeePanelLayout.createSequentialGroup()
                        .add(findEmployeePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(findLocationLabel)
                            .add(findNameLabel))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(findEmployeePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(findEmployeePanelLayout.createSequentialGroup()
                                .add(findNowhereRB)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(findLumberYardRB)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(findHardwareStoreRB))
                            .add(findNameTF)))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, findEmployeePanelLayout.createSequentialGroup()
                        .add(findButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(showAllbutton)))
                .addContainerGap(69, Short.MAX_VALUE))
        );
        findEmployeePanelLayout.setVerticalGroup(
            findEmployeePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(findEmployeePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(findDescriptionLabel)
                .add(18, 18, 18)
                .add(findEmployeePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(findNameLabel)
                    .add(findNameTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(findEmployeePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(findLocationLabel)
                    .add(findNowhereRB)
                    .add(findLumberYardRB)
                    .add(findHardwareStoreRB))
                .add(18, 18, 18)
                .add(findEmployeePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(findButton)
                    .add(showAllbutton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(findScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                .addContainerGap())
        );

        employeeTabbedPane.addTab("Søg", findEmployeePanel);

        getEmployeePanel.setBackground(new java.awt.Color(255, 255, 255));

        getDescriptionLabel.setText("Her kan der søges på ansattes ID");

        getIdLabel.setText("ID:");

        getButton.setText("Hent");
        getButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                getButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout getEmployeePanelLayout = new org.jdesktop.layout.GroupLayout(getEmployeePanel);
        getEmployeePanel.setLayout(getEmployeePanelLayout);
        getEmployeePanelLayout.setHorizontalGroup(
            getEmployeePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(getEmployeePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(getEmployeePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(getDescriptionLabel)
                    .add(getEmployeePanelLayout.createSequentialGroup()
                        .add(getIdLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(getIdSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(getButton))
                .addContainerGap(228, Short.MAX_VALUE))
        );
        getEmployeePanelLayout.setVerticalGroup(
            getEmployeePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(getEmployeePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(getDescriptionLabel)
                .add(18, 18, 18)
                .add(getEmployeePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(getIdLabel)
                    .add(getIdSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(getButton)
                .addContainerGap(249, Short.MAX_VALUE))
        );

        employeeTabbedPane.addTab("Hent", getEmployeePanel);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(employeeTabbedPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(employeeTabbedPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 369, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void getButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_getButtonActionPerformed
		Employee employee = this.controller.getEmployee(Integer.parseInt(getIdSP.getValue().toString()));
		if (employee != null) {
			new EmployeeInfoView(employee).setVisible(true);
		} else {
			JOptionPane.showMessageDialog(this, "Der er angivet et forkert ID.", "Forkert ID", JOptionPane.ERROR_MESSAGE);
		}
}//GEN-LAST:event_getButtonActionPerformed

	private void findButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findButtonActionPerformed
		Location location = null;
		if(findLumberYardRB.isSelected())
		{ location = Location.LumberYard; }
		else if(findHardwareStoreRB.isSelected())
		{ location = Location.HardwareStore; }
		this.findTable.setModel(
			new EmployeeTableModel(
				this.controller.findEmployee(findNameTF.getText(), location)));
}//GEN-LAST:event_findButtonActionPerformed

	private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
		if (!addNameTF.getText().equals(""))
		{
			Location location = null;
			if(addLumberYardRB.isSelected())
			{ location = Location.LumberYard; }
			else if(addHardwareStoreRB.isSelected())
			{ location = Location.HardwareStore; }
			this.controller.addEmployee(addNameTF.getText(), location);
			String locationString = location == Location.HardwareStore?"byggecenteret":"trælasthandelen";
			JOptionPane.showMessageDialog(this, addNameTF.getText()+" er oprettet som ansat i "+locationString+".", "Ansat oprettet", JOptionPane.INFORMATION_MESSAGE);
			addNameTF.setText("");
			addLumberYardRB.setSelected(true);
		}
		else
		{
			JOptionPane.showMessageDialog(this, "Intet navn er indtastet.", "Navn mangler", JOptionPane.WARNING_MESSAGE);
		}
}//GEN-LAST:event_addButtonActionPerformed

	private void showAllbuttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showAllbuttonActionPerformed
		findTable.setModel(new EmployeeTableModel(this.controller.getAllEmployee()));
}//GEN-LAST:event_showAllbuttonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JLabel addDescriptionLabel;
    private javax.swing.JPanel addEmployeePanel;
    private javax.swing.JRadioButton addHardwareStoreRB;
    private javax.swing.ButtonGroup addLocationBG;
    private javax.swing.JLabel addLocationLabel;
    private javax.swing.JRadioButton addLumberYardRB;
    private javax.swing.JLabel addNameLabel;
    private javax.swing.JTextField addNameTF;
    private javax.swing.JTabbedPane employeeTabbedPane;
    private javax.swing.JButton findButton;
    private javax.swing.JLabel findDescriptionLabel;
    private javax.swing.JPanel findEmployeePanel;
    private javax.swing.JRadioButton findHardwareStoreRB;
    private javax.swing.ButtonGroup findLocationBG;
    private javax.swing.JLabel findLocationLabel;
    private javax.swing.JRadioButton findLumberYardRB;
    private javax.swing.JLabel findNameLabel;
    private javax.swing.JTextField findNameTF;
    private javax.swing.JRadioButton findNowhereRB;
    private javax.swing.JScrollPane findScrollPane;
    private javax.swing.JTable findTable;
    private javax.swing.JButton getButton;
    private javax.swing.JLabel getDescriptionLabel;
    private javax.swing.JPanel getEmployeePanel;
    private javax.swing.JLabel getIdLabel;
    private javax.swing.JSpinner getIdSP;
    private javax.swing.JButton showAllbutton;
    // End of variables declaration//GEN-END:variables
}